package com.support.widget;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;

public class JumpManager {

    private static Handler handler_jump;

    public JumpManager() {
    }

    public static void gotoActivity(Context ctx, Class<? extends Activity> clazz, boolean finish) {
        Intent intent = new Intent((Activity) ctx, clazz);
        ((Activity) ctx).startActivity(intent);
        if (finish) {
            ((Activity) ctx).finish();
        }

        ctx = null;
    }

    public static void gotoActivity(Context ctx, Class<? extends Activity> clazz, boolean finish, int animIn, int animOut) {
        Intent intent = new Intent((Activity) ctx, clazz);
        ((Activity) ctx).startActivity(intent);
        ((Activity) ctx).overridePendingTransition(animIn, animOut);
        if (finish) {
            ((Activity) ctx).finish();
        }

        ctx = null;
    }

    public static void gotoActivityForResult(Context ctx, Class<? extends Activity> clazz, boolean finish, int animIn, int animOut, int requestCode) {
        Intent intent = new Intent((Activity) ctx, clazz);
        ((Activity) ctx).startActivityForResult(intent, requestCode);
        ((Activity) ctx).overridePendingTransition(animIn, animOut);
        if (finish) {
            ((Activity) ctx).finish();
        }

        ctx = null;
    }

    public static void gotoActivityForResult(Context ctx, Class<? extends Activity> clazz, boolean finish, Bundle pBundle, int requestCode) {
        Intent intent = new Intent((Activity) ctx, clazz);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        ((Activity) ctx).startActivityForResult(intent, requestCode);
        if (finish) {
            ((Activity) ctx).finish();
        }

        ctx = null;
    }

    public static void gotoActivityForResult(Context ctx, Class<? extends Activity> clazz, boolean finish, Bundle pBundle, int animIn, int animOut, int requestCode) {
        Intent intent = new Intent((Activity) ctx, clazz);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        ((Activity) ctx).startActivityForResult(intent, requestCode);
        ((Activity) ctx).overridePendingTransition(animIn, animOut);
        if (finish) {
            ((Activity) ctx).finish();
        }

        ctx = null;
    }

    public static void gotoActivityFromFragmentForResult(Fragment fragment, Class<? extends Activity> clazz, boolean finish, Bundle pBundle, int animIn, int animOut, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), clazz);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        fragment.startActivityForResult(intent, requestCode);
        fragment.getActivity().overridePendingTransition(animIn, animOut);
        if (finish) {
            fragment.getActivity().finish();
        }

    }

    public static void CountJump(final Context ctx, long mills, final Class<? extends Activity> clazz, final boolean finish) {
        handler_jump = new Handler();
        handler_jump.postDelayed(new Runnable() {
            public void run() {
                JumpManager.gotoActivity(ctx, clazz, finish);
            }
        }, mills);
    }

    public static void gotoActivity(Context ctx, Class<? extends Activity> clazz, boolean finish, Bundle pBundle) {
        Intent intent = new Intent((Activity) ctx, clazz);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        ((Activity) ctx).startActivity(intent);
        if (finish) {
            finish(ctx);
        }

        ctx = null;
    }

    public static void gotoActivity(Context ctx, Class<? extends Activity> clazz, boolean finish, Bundle pBundle, int animIn, int animOut) {
        Intent intent = new Intent((Activity) ctx, clazz);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        ((Activity) ctx).startActivity(intent);
        ((Activity) ctx).overridePendingTransition(animIn, animOut);
        if (finish) {
            finish(ctx);
        }

        ctx = null;
    }

    public static void gotoActivity(Context ctx, Class<? extends Activity> clazz, Bundle pBundle, int flags) {
        Intent intent = new Intent(ctx, clazz);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        intent.setFlags(flags);
        ctx.startActivity(intent);
        ctx = null;
    }

    public static void gotoActivity(Context context, String clsName) {
        Intent intent = new Intent();
        ComponentName name = new ComponentName(context.getPackageName(), clsName);
        intent.setComponent(name);
        context.startActivity(intent);
    }

    public static void finish(Context ctx, int animIn, int animOut) {
        ActivityStackManager.getScreenManager().popActivity((Activity) ctx);
        finish(ctx);
        ((Activity) ctx).overridePendingTransition(animIn, animOut);
        ctx = null;
    }

    public static void finish(Context ctx) {
        ActivityStackManager.getScreenManager().popActivity((Activity) ctx);
        ((Activity) ctx).finish();
        ctx = null;
    }

    public static void finishForResult(Context ctx, int resultCode) {
        ActivityStackManager.getScreenManager().popActivity((Activity) ctx);
        ((Activity) ctx).setResult(resultCode);
        ((Activity) ctx).finish();
        ctx = null;
    }

    public static void finishApplication() {
        ActivityStackManager.getScreenManager().popAllActivity();
    }

    public static void gotoMainFinishOthers(Context ctx, Class<? extends Activity> clazz, boolean finish, Bundle pBundle, int animIn, int animOut) {
        finishApplication();
        gotoActivity(ctx, clazz, finish, pBundle, animIn, animOut);
    }

}